#include "mesh.h"
#include <assimp\Importer.hpp>
#include <assimp\postprocess.h>
#include <assimp\scene.h>

#include "pg2_indexbuffer.h"
#include "vertexbuffer.h"

using namespace ns;

Mesh::Mesh(aiMesh* mesh , Renderer* _renderer){	
	/*Assimp::Importer importer;
	scene = importer.ReadFile(file,0x8|0x2);*/

	vertexAmmount = mesh->mNumVertices;
	vertices = new TextureVertex[vertexAmmount];
	
	for(int i = 0; i<vertexAmmount ;i++){
		vertices[i].x =mesh->mVertices[i].x;
		vertices[i].y =mesh->mVertices[i].y;
		vertices[i].z =mesh->mVertices[i].z;
		vertices[i].u =mesh->mTextureCoords[0][i].x;
		vertices[i].v = 1-mesh->mTextureCoords[0][i].y;
	//	vertices[i].color = PG2_COLOR_RGB(1,0,1);//PG2_COLOR_ARGB(0,1,1,1);
	}

	indexBuffer = new IndexBuffer(*_renderer,_renderer->getDevice());

	unsigned short* indices = new unsigned short[mesh->mNumFaces*3];

	int n = 0;

	for(unsigned int i = 0; i<mesh->mNumFaces;i++){
		for(unsigned int j = 0; j<mesh->mFaces[i].mNumIndices; j++){
			indices[n] = mesh->mFaces[i].mIndices[j];
			n++;
		}
	}

	indexBuffer->setIndexData(indices , mesh->mNumFaces*3);

	renderer = _renderer;

	delete indices;

	vertexBuffer=new VertexBuffer(*renderer,_renderer->getDevice() ,sizeof(TextureVertex), (D3DFVF_XYZ | D3DFVF_TEX1));
	vertexBuffer->setVertexData((const void*)vertices,vertexAmmount);

	bBox = new BoundingBox(this);
	bBoxVB = new VertexBuffer(*renderer,_renderer->getDevice() ,sizeof(ColorVertex), (D3DFVF_XYZ | D3DFVF_DIFFUSE));
	bBoxVB->setVertexData((const void*)bBox->GetVertices(),8);

	unsigned short* indicesVB = new unsigned short[24];
	indicesVB[0] = 0;
	indicesVB[1] = 1;
	indicesVB[2] = 1;
	indicesVB[3] = 2;
	indicesVB[4] = 2;
	indicesVB[5] = 3;
	indicesVB[6] = 3;
	indicesVB[7] = 0;
	indicesVB[8] = 6;
	indicesVB[9] = 5;
	indicesVB[10] = 5;
	indicesVB[11] = 4;
	indicesVB[12] = 4;
	indicesVB[13] = 7;
	indicesVB[14] = 7;
	indicesVB[15] = 6;
	indicesVB[16] = 0;
	indicesVB[17] = 7;
	indicesVB[18] = 3;
	indicesVB[19] = 4;
	indicesVB[20] = 1;
	indicesVB[21] = 6;
	indicesVB[22] = 2;
	indicesVB[23] = 5;
	
	bBoxIB = new IndexBuffer(*_renderer,_renderer->getDevice());
	bBoxIB->setIndexData(indicesVB,24);
	delete indicesVB;
	hasTexture = false;
}

void Mesh::SetBoundingBox(BoundingBox* box){bBox = box;}
void Mesh::SetRenderer(Renderer* r){renderer = r;}
void Mesh::SetIndexBuffer(IndexBuffer* ib){indexBuffer = ib;}
void Mesh::SetVertices(TextureVertex* v){vertices = v;}

void Mesh::Draw(){

	bBox->UpdateBB(*wordTransform);
	//bBoxVB->setVertexData((const void*)bBox->GetVertices(),8);

	if(!renderer->BoxInFrustum(bBox)){
		SetWindowTextA(renderer->GetWnd(),"No Dibuja");
		//return;
	}

	renderer->UpdateWorld(wordTransform);

	indexBuffer->bind();
	vertexBuffer->bind();

	if(hasTexture)
		renderer->SetTexture(texture);
	renderer->drawCurrentBuffers(TriangleList);

	
	
	bBoxIB->bind();
	bBoxVB->bind();

	renderer->drawCurrentBuffers(LineList);
	SetWindowTextA(renderer->GetWnd(),"Dibuja");

}

int Mesh::GetVertexAmmount(){
	return vertexAmmount;
}

TextureVertex* Mesh::GetVertices(){
	return vertices;
}

void Mesh::SetTexture(LPCSTR path){
	HRESULT hr = D3DXCreateTextureFromFile(renderer->getDevice(),path,&texture);
	if(hr == D3D_OK)
		hasTexture = true;
}